﻿using System.Collections.Generic;
using System.Data;
using KMF.Common;

namespace KMF.DAL
{
    public partial class TemplateDb
    {
        /// <summary>
        /// lấy tất cả các bản ghi trong bảng CategoryTemplate, có phân trang
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="itemsPerPage"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public static List<ViewCateTemplatePagingDto> GetAllPaging(int currentPage, int itemsPerPage, ref int totalPage)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Template_GetAllPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@CurrentPage", currentPage));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                var outPutTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                outPutTotalPage.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<ViewCateTemplatePagingDto>(ref command);

                totalPage = int.Parse(outPutTotalPage.Value.ToString() == "" ? "0" : outPutTotalPage.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy tất cả dữ liệu
        /// </summary>
        /// <returns></returns>
        public static List<TemplateDto> GetAll()
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Template_GetAll");
            try
            {
                return dao.GetDTOList<TemplateDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Thêm bản ghi mới và trả về dữ liệu của cột đầu tiên vừa mới thêm
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int Add(TemplateDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Template_Add");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Description", model.Description));
                command.Parameters.Add(dao.CreateParameter("@TempContent", model.TempContent));
                return (int)command.ExecuteScalar();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Lấy về một bản ghi theo Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static TemplateDto GetById(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Template_GetById");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", id));
                return dao.GetSingleDTO<TemplateDto>(ref command);
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int Update(TemplateDto model)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Template_Update");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", model.Id));
                command.Parameters.Add(dao.CreateParameter("@Description", model.Description));
                command.Parameters.Add(dao.CreateParameter("@TempContent", model.TempContent));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }

        /// <summary>
        /// Xoa ban ghi theo Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int Delete(int id)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Admin_Template_Delete");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Id", id));
                return command.ExecuteNonQuery();
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
